package com.example.alioss.controller;

import com.example.alioss.service.AsyncRequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping("/ajax")
public class AjaxController {

    @Autowired
    private AsyncRequestService asyncRequestService;

    @RequestMapping("/html")
    public String picUpload(Map<String, Object> map) {
        return "ajaxPage";
    }

    @ResponseBody
    @CrossOrigin
    @GetMapping("/getValue")
    public String getValue() {
        String msg = null;
        Future<String> result = null;
        try {
            result = asyncRequestService.getValue();
            msg = result.get(10, TimeUnit.SECONDS);
        } catch (Exception e) {
            // e.printStackTrace();
            msg = "redis没有数据！";
        } finally {
            if (result != null) {
                result.cancel(true);
            }
        }
        return msg;
    }

    @ResponseBody
    @CrossOrigin
    @GetMapping("/setValue")
    public String postValue(String msg) {
        System.out.println("postValue" + msg);
        return asyncRequestService.postValue(msg);
    }
}
